package pt.inescid.components.policyengine.heimdall.base;

import java.io.Serializable;
import java.util.Vector;

import pt.inescid.components.policyengine.heimdall.commons.TimeUtilities;
import pt.inescid.components.policyengine.heimdall.commons.Utilities;
import pt.inescid.components.policyengine.heimdall.core.MainConsole;
import pt.inescid.components.policyengine.heimdall.pap.UserManagerAS;
import pt.inescid.components.policyengine.heimdall.pap.base.ModelDefinition;

public class EventObj implements Serializable {

	
	private static final long serialVersionUID = 1L;
	private long id;
	private Vector fields;
	 
	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      Constructor       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public EventObj(long id, Vector allFields) {
		this(id, allFields, false, 0, null);
	}
	
	public EventObj(long id, Vector<RawEventField> allFields, boolean isRawData,
			long userId, String modelDefTag) {
		this.id = id;

		if (isRawData) {
			RawEventField currentRaw = null;
			EventField currentField = null;
			int currentFieldId = Utilities.UNDEFINED_INT_VALUE;
			this.fields = new Vector<RawEventField>();
			//FIXME no SQL DB!
			ModelDefinition modelDef = UserManagerAS.getRecord(1).getModelByTag(modelDefTag);
			
			for (int i = 0; i < allFields.size(); i++) {
				currentRaw = allFields.elementAt(i);
				currentFieldId = modelDef.getFieldId(currentRaw.getFieldName());
				
				if (currentFieldId != Utilities.UNDEFINED_INT_VALUE) {
					currentField = new EventField(
							currentFieldId,
							currentRaw.getValue());
					this.fields.add(currentField);
				}
			}
		}
		else {
			this.fields = allFields;
		}
	}
	
//	public EventObj(EventObj greaterEvent, 
//			Vector allRelevantEventFields,
//			Vector allRelevantCesParameters) {
//		
//		this.fields = new Vector();
//		this.parameters = new Vector();
//		
//		addCeInfo(fields, greaterEvent.getFields(), 
//				allRelevantEventFields);
//		addCeInfo(parameters, greaterEvent.getFields(), allRelevantCesParameters);
//	}
//	
//	
//	public void EventObj(Vector allInfo, 
//			Vector allRelevantEventFields,
//			Vector allRelevantCesParameters) {
//		
//		this.fields = new Vector();
//		this.parameters = new Vector();
//		
//		addCeInfo(fields, allInfo, allRelevantEventFields);
//		addCeInfo(parameters, allInfo, allRelevantCesParameters);
//	}

	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      getInfoFields       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public Vector getInfoFields(ModelDefinition modelDef) {
		Vector allInfoFields = new Vector(); 
		EventField currentField = null;
		
		for (int i = 0; i < fields.size(); i++) {
			currentField = (EventField) fields.elementAt(i);
			
			if (modelDef.isInfoField(currentField.getFieldId())) {
				allInfoFields.add(currentField);
			}
			
		}
		return (allInfoFields);
	} // getInfoFields

	public long getId() {
		return id;
	}

	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      addCeInfo       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
//	public void addCeInfo(Vector box, Vector allInfo, 
//			Vector allRelevantEventFields) {
//		
//		Vector allSingleRelevant = Utilities.removeDuplicates(
//				allRelevantEventFields);
//		
//		EventRelevance currentRelevance = null;
//		EventField currentField = null;
//		for (int i = 0; i < allSingleRelevant.size(); i++) {
//			currentRelevance = (EventRelevance) 
//						allSingleRelevant.elementAt(i);
//			
//			for (int j = 0; j < allInfo.size(); j++) {
//				currentField = (EventField) allInfo.elementAt(j);
//				
//				if (currentField.getFieldId() == 
//					currentRelevance.getFieldId()) {
//					
//					box.add(currentField);
//				}
//			}
//		}
//	} // addCeInfo
//
	
	
//	
//	public Vector getParameters() {
//		return parameters;
//	}
//
	public void addField(EventField newField) {
		this.fields.add(newField);
	}

	
	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      getField       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public EventField getField(int fieldId) {

		EventField currentField = null;				
		for (int i = 0; i < fields.size(); i++) {
			currentField = (EventField) fields.elementAt(i);
			
			if (currentField.getFieldId() == fieldId) {
				return (currentField);
			}
		}
		return (null);
	}
	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      updateField       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public void updateField(int fieldId, Object newValue) {

		EventField currentField = null;
		for (int i = 0; i < fields.size(); i++) {
			currentField = (EventField) fields.elementAt(i);
			
			if (currentField.getFieldId() == fieldId) {
				currentField.setValue(newValue);
			}
		}
	}
	
	/* ===========================================================
	==============================================================
	==============================================================
	getTestEvent
	==============================================================
	==============================================================
	=========================================================== */
	public static EventObj getTestEvent(User currentUser) {

		return (getTestEvent(currentUser, "Storage"));
	} // getTestEvent

	
	
	/* ===========================================================
	==============================================================
	==============================================================
	getTestEvent
	==============================================================
	==============================================================
	=========================================================== */
	public static EventObj getTestEvent(User currentUser, 
			String action) {

		Vector<RawEventField> allFields = new Vector<RawEventField>();
		allFields.add(new RawEventField(".user.name", "pgama"));
		allFields.add(new RawEventField(".user.company.name", "INESC-ID"));
		allFields.add(new RawEventField(".action.name", action));
		allFields.add(new RawEventField(".action.value", new Integer(1)));
		allFields.add(new RawEventField(".action.parameter[0]", "BEGIN"));
		allFields.add(new RawEventField(".executionTime", 
				new Long(TimeUtilities.getCurrentTime())));
		
		EventObj testObj = new EventObj(100, allFields, true,
				currentUser.getId(), MainConsole.MODEL_TAG);

		return (testObj);

//		SplUser thisCompany = new SplUser(1, "INESC-ID", null, 0, 
//				SplUser.USER_TYPE_COMPANY);
//		SplUser thisUser = new SplUser(2, "pgama", thisCompany, 0, 
//				SplUser.USER_TYPE_INDIVIDUAL);
//		Vector actionParameters = new Vector();
//		actionParameters.add("BEGIN");
//		SplAction thisAction = new SplAction(1, action, actionParameters);
//		thisAction.setValue(2);
//		long[] time = new long[5];
//		time[SplEvent.EVENT_EXECUTION_TIME] = TimeUtilities.getCurrentTime();
//		
//		SplEvent ce = new SplEvent(100, thisUser, 
//				null, thisAction, time, "argentina.gsd.inesc-id.pt");
//
//		return (ce);
		
	} // getTestEvent


	

	
	
	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      toString       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public String toString() {
		String result = "";
	
		result += "id=" + id + " ";
		
		if (fields != null) {
			EventField currentParameter = null;
			for (int i = 0; i < fields.size(); i++) {
				currentParameter = (EventField) fields.elementAt(i);
				
				result += currentParameter + " ";
			}
		}
		
		return (result);
	} // toString

//	public String getTags() {
//		return tags;
//	}
//
//	public void setTags(String tags) {
//		this.tags = tags;
//	}
//
//	public Vector getInfo() {
//		return info;
//	}
//
//	public void setInfo(Vector info) {
//		this.info = info;
//	}
}; // EventObject
